package cn.tedu._10article.controller;

import cn.tedu._10article.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/v1/file/")
public class UploadController {
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        String dirPath = "d:files";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd");
        String datePath = sdf.format(new Date());
        File f = new File(dirPath+datePath);
        if (!f.exists()) {
            f.mkdirs();
        }
        String filePath = dirPath+datePath+fileName;
        file.transferTo(new File(filePath));
        log.debug("原始文件名"+datePath);
        return  JsonResult.ok(datePath+fileName);
    }
}
